package com.design.twords.activity;

import android.os.Bundle;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;

import com.design.twords.R;
import com.design.twords.config.ConfigData;
import com.design.twords.utils.MyApplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Create by Alen
 * Time:  14:24
 */
public class ListenSetActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView back;
    private EditText repeattimes, spacetime;
    private RadioGroup songset, playset;
    private RadioButton ussong, uksong, order, random;
    private Switch showtrans;
    private Button save;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listenset);
        MyApplication.getInstance().addActivity(this);
        initView();
        initData();

        //简单限定输入数字
        spacetime.setKeyListener(DigitsKeyListener.getInstance("12345"));
        repeattimes.setKeyListener(DigitsKeyListener.getInstance("2345"));


    }

    private void initData() {
        repeattimes.setText(String.valueOf(ConfigData.getRepeatTimes()));
        spacetime.setText(String.valueOf(ConfigData.getSpaceTime()));
        int song = ConfigData.getSong();
        if (song  == ConfigData.ENGLISH_VOICE)
            uksong.setChecked(true);
        else
            ussong.setChecked(true);
        int playmode = ConfigData.getPlaymode();
        if (playmode  == ConfigData.ORDER_PLAY)
            order.setChecked(true);
        else
            random.setChecked(true);

        boolean showTrans = ConfigData.isShowTrans();
        showtrans.setChecked(showTrans);
    }

    private void initView() {
        back = findViewById(R.id.listenset_back);
        repeattimes = findViewById(R.id.listenset_repeattimes);
        spacetime = findViewById(R.id.listenset_spacetime);
        songset = findViewById(R.id.listenset_songset);
        ussong = findViewById(R.id.listenset_ussong);
        uksong = findViewById(R.id.listenset_uksong);
        showtrans = findViewById(R.id.listenset_showtrans);
        save = findViewById(R.id.listenset_save);
        playset = findViewById(R.id.listenset_playset);
        order = findViewById(R.id.listenset_order);
        random = findViewById(R.id.listenset_random);


        back.setOnClickListener(this);
        save.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.listenset_back:
                onBackPressed();
                finish();
                break;
            case R.id.listenset_save:
                String rtt = repeattimes.getText().toString();
                String st = spacetime.getText().toString();

                if (rtt.isEmpty() || st.isEmpty()) {
                    Toast.makeText(ListenSetActivity.this, "次数或间隔不可为空", Toast.LENGTH_SHORT).show();
                    break;
                }

                ConfigData.setRepeatTimes(Integer.parseInt(rtt));
                ConfigData.setSpaceTime(Integer.parseInt(st));
                ConfigData.setShowTrans(showtrans.getShowText());
                switch (songset.getCheckedRadioButtonId()){
                    case R.id.listenset_ussong:
                        ConfigData.setSong(ConfigData.AMERICA_VOICE);
                        break;
                    case R.id.listenset_uksong:
                        ConfigData.setSong(ConfigData.ENGLISH_VOICE);
                        break;
                }
                if (playset.getCheckedRadioButtonId() == R.id.listenset_order)
                    ConfigData.setPlaymode(ConfigData.ORDER_PLAY);
                else
                    ConfigData.setPlaymode(ConfigData.RANDOM_PLAY);
                Toast.makeText(ListenSetActivity.this, "保存成功", Toast.LENGTH_LONG).show();
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstance().removeActivity(this);
    }
}
